using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace SPWorkflowStringActions.Features.SPWorkflowStringActionsFeatureReceiver
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("1dfc5cbe-f101-4fd3-9bf5-fdad7b1dc675")]
    public class SPWorkflowStringActionsFeatureReceiverEventReceiver : SPFeatureReceiver
    {
        const string webConfigModificationName = "authorizedType[@Assembly='SPWorkflowStringActions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3825586d873cffa1'][@Namespace='SPWorkflowStringActions'][@TypeName='*'][@Authorized='True']";
        const string webConfigModificationOwner = "SPWorkflowStringActions";

        /// <summary>
        /// Runs when SPWorkflowStringActions feature is activated on a SPWebApplication
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebApplication thisWebApp = (SPWebApplication)properties.Feature.Parent;

                SPWebConfigModification thisModification = new SPWebConfigModification();
                thisModification.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes/targetFx";
                thisModification.Name = webConfigModificationName;
                thisModification.Sequence = 0;
                thisModification.Owner = webConfigModificationOwner;
                thisModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                thisModification.Value = "<authorizedType Assembly=\"SPWorkflowStringActions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3825586d873cffa1\" Namespace=\"SPWorkflowStringActions\" TypeName=\"*\" Authorized=\"True\" />";

                thisWebApp.WebConfigModifications.Add(thisModification);
                thisWebApp.Update();

                thisWebApp.WebService.ApplyWebConfigModifications();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("SPWorkflowStringActionsFeatureActivatedReceiver", ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Runs when SPWorkflowStringActions feature is deactivated on a SPWebApplication
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try 
            { 
                SPWebApplication thisWebApp = (SPWebApplication)properties.Feature.Parent;
                Collection<SPWebConfigModification> modsCollection = thisWebApp.WebConfigModifications;

                bool foundMod = false;

                for (int i = 0; i < modsCollection.Count; i++)
                {
                    if (modsCollection[i].Owner == webConfigModificationOwner)
                    {
                        thisWebApp.WebConfigModifications.Remove(modsCollection[i]);
                        foundMod = true;
                    }
                }

                if (foundMod)
                {
                    thisWebApp.Update();
                    thisWebApp.WebService.ApplyWebConfigModifications();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("SPWorkflowStringActionsFeatureActivatedReceiver", ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
